﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Flight_Entity : EntityBase<FLIGHT>
    {
        public override RESP_Info Add(FLIGHT model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into FLIGHT(FLIGHT_ID,FLIGHT_CODE,FLIGHT_CITY_START,FLIGHT_CITY_END,FLIGHT_TIME_START,FLIGHT_TIME_END,FLIGHT_PRICE) values (@FLIGHT_ID,@FLIGHT_CODE,@FLIGHT_CITY_START,@FLIGHT_CITY_END,@FLIGHT_TIME_START,@FLIGHT_TIME_END,@FLIGHT_PRICE)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", model.FLIGHT_ID));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_CODE", model.FLIGHT_CODE));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_CITY_START", model.FLIGHT_CITY_START));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_CITY_END", model.FLIGHT_CITY_END));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_TIME_START", model.FLIGHT_TIME_START));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_TIME_END", model.FLIGHT_TIME_END));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_PRICE", model.FLIGHT_PRICE));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加航班成功</p></h5>";
                    info.Url = "FlightList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加航班失败</p></h5>";
                    info.Url = "FlightAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(FLIGHT model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update FLIGHT set FLIGHT_CODE=@FLIGHT_CODE,FLIGHT_CITY_START=@FLIGHT_CITY_START,FLIGHT_CITY_END=@FLIGHT_CITY_END,FLIGHT_TIME_START=@FLIGHT_TIME_START,FLIGHT_TIME_END=@FLIGHT_TIME_END,FLIGHT_PRICE=@FLIGHT_PRICE where FLIGHT_ID=@FLIGHT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", model.FLIGHT_ID));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_CODE", model.FLIGHT_CODE));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_CITY_START", model.FLIGHT_CITY_START));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_CITY_END", model.FLIGHT_CITY_END));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_TIME_START", model.FLIGHT_TIME_START));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_TIME_END", model.FLIGHT_TIME_END));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_PRICE", model.FLIGHT_PRICE));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑航班成功</p></h5>";
                    info.Url = "FlightList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑航班失败</p></h5>";
                    info.Url = "FlightAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(FLIGHT flight)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from FLIGHT where FLIGHT_ID=@FLIGHT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", flight.FLIGHT_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除航班成功</p></h5>";
                    info.Url = "FlightList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除航班失败</p></h5>";
                    info.Url = "FlightList.aspx";

                    Tom.Net.Log.LogManager.Error("删除航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override FLIGHT GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select FLIGHT_ID,FLIGHT_CODE,FLIGHT_CITY_START,FLIGHT_CITY_END,FLIGHT_TIME_START,FLIGHT_TIME_END,FLIGHT_PRICE from FLIGHT where FLIGHT_ID=@FLIGHT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        FLIGHT flight = new FLIGHT();
                        if (!reader.IsDBNull(0)) flight.FLIGHT_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) flight.FLIGHT_CODE = reader.GetString(1);
                        if (!reader.IsDBNull(2)) flight.FLIGHT_CITY_START = reader.GetString(2);
                        if (!reader.IsDBNull(3)) flight.FLIGHT_CITY_END = reader.GetString(3);
                        if (!reader.IsDBNull(4)) flight.FLIGHT_TIME_START = reader.GetDateTime(4);
                        if (!reader.IsDBNull(5)) flight.FLIGHT_TIME_END = reader.GetDateTime(5);
                        if (!reader.IsDBNull(6)) flight.FLIGHT_PRICE = reader.GetInt32(6);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return flight;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override FLIGHT GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        FLIGHT flight = new FLIGHT();
                        if (!reader.IsDBNull(0)) flight.FLIGHT_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) flight.FLIGHT_CODE = reader.GetString(1);
                        if (!reader.IsDBNull(2)) flight.FLIGHT_CITY_START = reader.GetString(2);
                        if (!reader.IsDBNull(3)) flight.FLIGHT_CITY_END = reader.GetString(3);
                        if (!reader.IsDBNull(4)) flight.FLIGHT_TIME_START = reader.GetDateTime(4);
                        if (!reader.IsDBNull(5)) flight.FLIGHT_TIME_END = reader.GetDateTime(5);
                        if (!reader.IsDBNull(6)) flight.FLIGHT_PRICE = reader.GetInt32(6);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return flight;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<FLIGHT> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<FLIGHT> flights = new List<FLIGHT>();
                    while (reader.Read())
                    {
                        FLIGHT flight = new FLIGHT();
                        if (!reader.IsDBNull(0)) flight.FLIGHT_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) flight.FLIGHT_CODE = reader.GetString(1);
                        if (!reader.IsDBNull(2)) flight.FLIGHT_CITY_START = reader.GetString(2);
                        if (!reader.IsDBNull(3)) flight.FLIGHT_CITY_END = reader.GetString(3);
                        if (!reader.IsDBNull(4)) flight.FLIGHT_TIME_START = reader.GetDateTime(4);
                        if (!reader.IsDBNull(5)) flight.FLIGHT_TIME_END = reader.GetDateTime(5);
                        if (!reader.IsDBNull(6)) flight.FLIGHT_PRICE = reader.GetInt32(6);

                        flights.Add(flight);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return flights;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
